package haxe.util.benchmark;

class BenchmarkHelper
{
	var iterations:Int;
	var r:Array<BenchmarkResult>;
	public var results(getResults,null):Array<BenchmarkResult>;
	var currentAction:String;
	var time:Int;
	
	private function new( n:Int )
	{
		this.iterations = n;
		r = new Array<BenchmarkResult>();
	}
	
	public function init( action:String ):Void
	{
		this.currentAction = action;
		time = Timer.getTime();
	}
	
	public function done():Void
	{
		r.push( new BenchmarkResult( this.currentAction, Timer.getTime() - time ) );
	}
	
	public function run():Void
	{
		
		var fields:Array<String> = Type.getInstanceFields( Type.getClass(this) );
		for ( i in fields )
		{
			//trace("test : "+i);
			if( i != "run" && StringTools.startsWith(i,"run" ) && Reflect.isFunction( Reflect.field( this, i ) ) )
			{
				Reflect.callMethod( this, Reflect.field( this, i ), [] );
				BenchmarkDisplay.display( r[ r.length - 1] );
				BenchmarkDisplay.display( r[ r.length - 2] );
				BenchmarkDisplay.display( r[ r.length - 3] );
			}
		}
	}
	
	public function getResults():Array<BenchmarkResult>
	{
		return r;
	}
}